package com.nanyang.tms.manager.pick.impl;

import cn.hutool.core.util.ObjectUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.annotation.SystemMessage;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.company.CompanyDao;
import com.nanyang.tms.dao.company.CompanyPickAddressDao;
import com.nanyang.tms.dao.pick.PickGoodsDao;
import com.nanyang.tms.domain.company.CompanyPickAddressQuery;
import com.nanyang.tms.domain.company.CompanyPickAddressVO;
import com.nanyang.tms.domain.pick.PickGoodsDO;
import com.nanyang.tms.domain.pick.PickGoodsQuery;
import com.nanyang.tms.domain.pick.PickGoodsVO;
import com.nanyang.tms.manager.pick.PickGoodsManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component("pickGoodsManager")
public class PickGoodsManagerImpl implements PickGoodsManager {
    @Autowired
    private PickGoodsDao pickGoodsDao;
    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;
    @Autowired
    private CompanyPickAddressDao companyPickAddressDao;
    @Autowired
    private CompanyDao companyDao;
    @Override
    public PageBean<PickGoodsVO> listPickGoods(PickGoodsQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = pickGoodsDao.countPickGoods(query);
            PageBean<PickGoodsVO> page = new PageTableBean<PickGoodsVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(pickGoodsDao.listPickGoods(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @SystemMessage(messageType=SystemMessage.MessageType.SERVICE,template="pickAddMsg")
    @SystemLog(module =SystemLog.Module.COMPANY,template="pickAdd")
    @Override
    public void savePickGoods(PickGoodsVO pickGoodsVO) {
        try {
            CompanyPickAddressQuery query=new CompanyPickAddressQuery();
            query.setCompanyCode(pickGoodsVO.getCompanyCode());
            query.setPickCode(pickGoodsVO.getPickAddressCode());
            CompanyPickAddressVO companyPickAddressVO = companyPickAddressDao.selectByCode(query);
            if (ObjectUtil.isEmpty(companyPickAddressVO)) {
                throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            PickGoodsDO pickGoodsDO = new PickGoodsDO();
            BeanUtils.copyProperties(pickGoodsVO,pickGoodsDO);
            BeanUtils.copyProperties(companyPickAddressVO,pickGoodsDO);
            pickGoodsDO.setContactTel(companyPickAddressVO.getTel());
            pickGoodsDO.setCompanyName(companyDao.selectByCode(pickGoodsDO.getCompanyCode()).getCompanyName());
            pickGoodsDO.setPickCode(TmsSerializeConstant.PICK_GOODS_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.PICK_GOODS_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY));
            pickGoodsDao.insert(pickGoodsDO);
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
}
